import {CascaderProps} from "element-plus";
import {ElFormItem} from "element-plus/es/components/form";
import DynamicForm from './index.vue'
import FromItem from './FromItem.vue'
import {FormTypes} from "./componentsConfig";
import {ElDatePicker} from "element-plus/es/components/date-picker";
import {FormItemRule} from "element-plus/es/tokens/form";

export type DynamicForm3InstanceType = InstanceType<typeof DynamicForm>
export type FromItem3InstanceType = InstanceType<typeof FromItem>

export interface OptionsProps extends CascaderProps {

}

export interface EventFirstParam {
  formData: object;
  item: FormConfigItem
}

export type ComProps = {
  clearable?: boolean;
  multiple?: boolean;
  group?: boolean;
  options?: Record<string, unknown>[];
  props?: OptionsProps;
  placeholder?: string;

  [key: string]: any;
} & InstanceType<typeof ElDatePicker>

export interface ComEvents {
  [key: string]: any;
}

// @ts-ignore
export interface ComFormItemRule extends FormItemRule {
  validator?: (rule: ComFormItemRule, value: any, callback: (error?: string | Error) => void, formData: object, item: FormConfigItem) => void;
  asyncValidator?: (rule: ComFormItemRule, value: any, formData: object, item: FormConfigItem) => Error | Promise<string>;
}

export type HiddenFunction = (item: FormConfigItem) => boolean;

export type FormConfigItem = {
  type: FormTypes;
  prop: string;
  /**
   * 为了方便，存在prop2
   */
  prop2?: string;
  label: string;
  /**
   * 组件插槽的前缀名字
   */
  slotName?: string;
  /**
   * formItem的插槽的名字
   */
  labelSlotName?: string;
  /**
   * formItem的插槽的名字
   */
  errorSlotName?: string;
  span?: number;
  /**
   * 第一次创建组件初始化组件的值
   */
  initValue?: any;
  /**
   * 隐藏
   */
  hidden?: boolean | HiddenFunction;
  /**
   * 隐藏占位
   */
  hiddenSeat?: boolean;
  /**
   * 在search的时候是否开启变化搜索，
   * 每个组件自定义实现不一样，
   * 但是都是调用ComsCommonMixin混入的isSearchEmitSearch方法
   */
  search?: boolean;
  props?: ComProps;
  events?: ComEvents;
  /**
   * 是否必填
   */
  required?: boolean;
  /**
   * 定义检验
   */
  rules?: ComFormItemRule[]

  /**
   * 插槽初始化值的方法
   * @param formData
   * @param item
   */
  slotInitValue?: (formData: Object, item: FormConfigItem) => void;
} & InstanceType<typeof ElFormItem>
